package com.ht.system.controller;
import java.util.Date;
import java.util.List;
import java.io.IOException;

import com.ht.common.core.domain.R;
import com.ht.common.core.web.domain.AjaxResult;
import javax.servlet.http.HttpServletResponse;

import com.ht.common.security.annotation.InnerAuth;
import com.ht.system.api.dto.CustomsDto;
import com.ht.system.api.dto.DictDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ht.common.log.annotation.Log;
import com.ht.common.security.utils.SecurityUtils;
import com.ht.common.core.convert.EntitySelectConverter;
import com.ht.common.core.domain.vo.EntitySelectVo;
import com.ht.common.core.utils.SnowflakeIdWorker;
import com.ht.common.log.enums.BusinessType;
import com.ht.common.security.annotation.RequiresLogin;
import com.ht.common.security.annotation.RequiresPermissions;
import com.ht.system.domain.BaseFieldInfo;
import com.ht.system.domain.Customs;
import com.ht.system.service.ICustomsService;
import com.ht.common.core.web.controller.BaseController;
import com.ht.common.core.web.domain.AjaxResult;
import com.ht.common.core.utils.poi.ExcelUtil;
import com.ht.common.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.ht.common.core.web.page.TableDataInfo;

/**
 * 关区代码Controller
 *
 * @author zmx
 * @date 2025-03-22
 */
 @Api(tags = "关区代码接口")
@RestController
@RequestMapping("/customs")
public class CustomsController extends BaseController
{
    @Autowired
    private ICustomsService customsService;

    /**
     * 查询关区代码列表
     */
    @ApiOperation("列表,权限:system:customs:list")
    @RequiresPermissions("system:customs:list")
    @GetMapping("/list")
    public TableDataInfo list(Customs customs)
    {
        startPage();
        List<Customs> list = customsService.selectCustomsList(customs);
        return getDataTable(list);
    }

    @ApiOperation("下来列表不分页")
    @RequiresLogin
    @GetMapping("/findList")
    public AjaxResult findList(Customs customs)
    {
    	List<Customs> list = customsService.selectCustomsList(customs);
    	List<EntitySelectVo> result= EntitySelectConverter.convert(
    			list,
    			Customs::getCustomsCode,
    			Customs::getCustomsName
    		);
    	return AjaxResult.success(result);
    }



    /**
     * 获取关区代码详细信息
     */
    @ApiOperation("详细信息,权限:system:customs:list")
    @RequiresPermissions("system:customs:list")
    @GetMapping(value = "/getInfo/{customsCode}")
    public AjaxResult getInfo(@PathVariable("customsCode") String customsCode)
    {
        return AjaxResult.success(customsService.selectCustomsByCustomsCode(customsCode));
    }

    /**
     * 新增关区代码
     */
    @ApiOperation("新增,权限:system:customs:add")
    @RequiresPermissions("system:customs:add")
    @Log(title = "关区代码", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody Customs customs)
    {
        return toAjax(customsService.insertCustoms(customs));
    }

    /**
     * 修改关区代码
     */
    @ApiOperation("修改,权限:system:customs:edit")
    @RequiresPermissions("system:customs:edit")
    @Log(title = "关区代码", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody Customs customs)
    {
        return toAjax(customsService.updateCustoms(customs));
    }

    /**
     * 删除关区代码
     */
    @ApiOperation("删除,权限:system:customs:remove")
    @RequiresPermissions("system:customs:remove")
    @Log(title = "关区代码", businessType = BusinessType.DELETE)
	@GetMapping("remove/{customsCodes}")
    public AjaxResult remove(@PathVariable("customsCodes") String[] customsCodes)
    {
        return toAjax(customsService.deleteCustomsByCustomsCodes(customsCodes));
    }


    @InnerAuth
    @GetMapping("findByCustomsCode/{customsCode}")
    public R<CustomsDto> findByCustomsCode(@PathVariable("customsCode") String customsCode)
    {
        Customs customs=  customsService.selectCustomsByCustomsCode(customsCode);
        return R.ok(toDto(customs));
    }

    private CustomsDto toDto(Customs customs) {
        CustomsDto ct=new CustomsDto();
        ct.setCustomsCode(customs.getCustomsCode());
        ct.setCustomsName(customs.getCustomsName());
        ct.setParentCustomCode(customs.getParentCustomCode());
        ct.setParentCustomName(customs.getParentCustomName());
        return ct;
    }

}
